//
//  KeHuViewController.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/3/3.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "KeHuViewController.h"

#import "KeDetailsCell.h"
#import "TodoHttpManager.h"

#import "KeHuXVC.h"
#import "FTPopOverMenu.h"
@interface KeHuViewController ()<UITableViewDelegate,UITableViewDataSource>

{
    NSMutableArray *_keDateArr;
    NSString *uid;
    
    
  
}
@property(nonatomic,strong)UITableView *tableView;

@end

@implementation KeHuViewController

-(void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDYJke:) name:TDYJke object:nil];
    NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];
    
    uid= [userDefa objectForKey:@"USER_ID"];
    [_manager userGMYongJinKeHuWithID:uid withHuiK:nil withYongJ:nil withSouSuoT:nil withSouSuo:nil];
}

-(void)didTDYJke:(NSNotification *)notifi
{
    if (notifi.object != nil)
    {
        int status = [[notifi.object objectForKey:@"status"] intValue];
        if (status == 1)
        {
            _keDateArr = [notifi.object objectForKey:@"data"];
            [_tableView reloadData];
        }
        else
        {
            [_manager MakeMyselfAlert:@"获取失败" andIsNeedTitle:NO andShowTime:2];
        }
    }

}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:TDYJke object:nil ];
    //第四步,消息发送完,要移除掉
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"cesuo" object:nil];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BgColor;
    _keDateArr = [NSMutableArray arrayWithCapacity:0];
    _manager = [TodoHttpManager sharedManager];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, B_HEIGHT-158) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 60;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];


    //第二步,通知中心,发送一条消息通知----------其中name名字千万不要写错了,会出现在3个地方
    //通知中心是个单例
    NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
    [notiCenter addObserver:self selector:@selector(receiveNotification:) name:@"cesuo" object:nil];
    
    NSNotificationCenter *seacherCenter = [NSNotificationCenter defaultCenter];
    [seacherCenter addObserver:self selector:@selector(seacherCenterNotification:) name:@"SacherBar" object:nil];

}

#pragma mark-右上角按钮
- (void)receiveNotification:(NSNotification *)noti
{
    
    // NSNotification 有三个属性，name, object, userInfo，其中最关键的object就是从第三个界面传来的数据。name就是通知事件的名字， userInfo一般是事件的信息。
    NSLog(@"%@ === %@ === %@", noti.object, noti.userInfo, noti.name);
    NSString *oneStr = [noti.object objectForKey:@"AA"];
    NSString *twoStr = [noti.object objectForKey:@"BB"];
    NSLog(@"------11111------%@",oneStr);
    NSLog(@"---------22222-------%@",twoStr);
    [_manager userGMYongJinKeHuWithID:uid withHuiK:oneStr withYongJ:twoStr withSouSuoT:@"2" withSouSuo:nil];
    
}
#pragma mark-搜索的事件
-(void)seacherCenterNotification:(NSNotification *)notifi
{
    NSLog(@"搜索的内容-----%@",notifi.object);
    NSString *nameStr = notifi.object;
    [_manager userGMYongJinKeHuWithID:uid withHuiK:nil withYongJ:nil withSouSuoT:nil withSouSuo:nameStr];
    NSLog(@"=======%@",nameStr);
    [_tableView reloadData];
    if ([notifi.object isEqualToString:@""])
    {
        [_manager userGMYongJinKeHuWithID:uid withHuiK:nil withYongJ:nil withSouSuoT:nil withSouSuo:nil];

        [_tableView reloadData];
    }
}

//- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
//{
//    //NSString * getStr = searchBar.text;
//    
//    if (searchText.length == 0)
//    {
//        [_manager userGMYongJinKeHuWithID:uid withHuiK:nil withYongJ:nil withSouSuoT:nil withSouSuo:nil];
//        
//        
//        [_tableView reloadData];
//    }
//    else
//    {
//        [_manager userGMYongJinKeHuWithID:uid withHuiK:nil withYongJ:nil withSouSuoT:nil withSouSuo:searchText];
//        //更新UI
//        [_tableView reloadData];
//    }
//}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _keDateArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndent = @"Cell";
    KeDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndent];
    if (!cell)
    {
        cell = [[KeDetailsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndent];
        
    }
    if ([cell isKindOfClass:[KeDetailsCell class]])
    {
        if (_keDateArr.count != 0)
        {
            NSDictionary *dict = [_keDateArr objectAtIndex:indexPath.row];
            
            [(KeDetailsCell *)cell setUpCellKeLieWithName:dict[@"rengou_name"] andHouse:dict[@"room_num"] andDate:dict[@"sign_time"] andHuiSty:dict[@"hk_status"] andYongMoney:dict[@"jq_status"] andPhone:dict[@"rengou_phone"]];
            
        }
    }
    

    
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone];//取消点击
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    KeHuXVC *keVC = [[KeHuXVC alloc] init];
    keVC.keID = [[_keDateArr objectAtIndex:indexPath.row] objectForKey:@"id"];
    keVC.Type = [[_keDateArr objectAtIndex:indexPath.row] objectForKey:@"type"];
    [self.navigationController pushViewController:keVC animated:NO];
    
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}









@end
